package fnord.network;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import fnord.exception.*;
import fnord.illuminati.Board;
import fnord.jovian.*;
import fnord.network.client.Client;
import fnord.network.server.Server;

/**
 * This class is a package tester class. It tests if creating and connecting within the network works
 * @author istrickman
 *
 */
public class NetworkMain {

	
	private class ServerThread extends Thread {
		private Server _running;
		private int _numPlayers, _port;
		private Object _isReady;
		private Object _readyWithClients;
		private boolean _lockedClients;
		
		public ServerThread(int players, int port) {
			_numPlayers = players;
			_port = port;
			_isReady = new Object();
			_readyWithClients = new Object();
			_lockedClients = false;
		}
		
		public void run() {
			try {
				synchronized(_isReady) {
					_running = new Server(_numPlayers, _port);
					_isReady.notifyAll();
				}
				//notifyAll();
				synchronized(_readyWithClients) {
					_running.getClients();
					//_readyWithClients = true;
					_lockedClients = true;
					_readyWithClients.notifyAll();
				}
			} catch (BadPortException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (MapLoaderException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		public Server getServer() { 
			
			synchronized(_isReady) {
				try {
					_isReady.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			/*while(!_isReady) {
				try {
					wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				//System.out.println("FDSF");
			}*/
			return _running; 
		}
		
		public void sendChat(String s) {
			synchronized(_readyWithClients) {
				if(!_lockedClients) {
					try {
						_readyWithClients.wait();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			_running.sendChat(s);
		}

		public void sendQuit() {
			_running.sendQuit();
			
		}
	}
	
	private class ClientThread extends Thread {
		@SuppressWarnings("unused")
		private Client _running;
		private int _port;
		private String _host;
		
		public ClientThread(int port, String host) {
			_host = host;
			_port = port;
		}
		
		public void run() {
			try {
				_running = new Client(_host, _port, new NetworkGUICommunicator());
			} catch (BadPortException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}		
	}
	
	private class NetworkGUICommunicator extends TheGUICommunicator {

		public NetworkGUICommunicator() {
			super(null);
			// TODO Auto-generated constructor stub
		}
		
		@Override
		public void takeBoard(Board b) {
		}
		
		@Override
		public boolean takeChat(String s) {
			return false;
		}
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
//		System.out.println("starting");
		NetworkMain nm = new NetworkMain();
		ServerThread serverThread = nm.new ServerThread(2, 0);
		serverThread.start();
		int port  = serverThread.getServer().getPort();
//		System.out.println(port);
		ClientThread clientThread = nm.new ClientThread(port, "localhost");
		clientThread.start();
		ClientThread clientThread2 = nm.new ClientThread(port, "localhost");
		clientThread2.start();
		BufferedReader pr = new BufferedReader(new InputStreamReader(System.in));
		while(true) {
			try {
				String s = pr.readLine();
				if(s.equals("exit")) {
					serverThread.sendQuit();
					System.exit(0);
				}
				serverThread.sendChat(s);
			} catch (IOException e) {
				System.err.println("WELL FUCK");
			}
		}
			

	}

}
